package com.sise.dubbo.rpc;

import com.sise.dubbo.api.UserRpcService;
import com.sise.dubbo.common.PaginationRespBO;
import com.sise.dubbo.common.ServiceException;
import com.sise.dubbo.dto.PaginationRespDTO;
import com.sise.dubbo.dto.RpcRespDTO;
import com.sise.dubbo.model.User;
import com.sise.dubbo.service.TestUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author hao_lin
 * @data 2018/9/29 10:48
 */
@Service("userRpcService")
@Slf4j
public class UserRpcServiceImpl implements UserRpcService {

    @Autowired
    private TestUserService testUserService;

    @Override
    public String printStr(String str) {
        System.out.println("this is printStr");
        return str;
    }

    @Override
    public RpcRespDTO<PaginationRespDTO<User>> findAll(int page) {
        try {
            PaginationRespBO<User> userList = testUserService.findAll(page);
            PaginationRespDTO<User> userPaginationRespDTO = new PaginationRespDTO<User>();
            userPaginationRespDTO.setList(userList.getList());
            userPaginationRespDTO.setTotal(userList.getTotal());
            return new RpcRespDTO().buildSuccessResp(userPaginationRespDTO);
        } catch (Exception e) {
            return new RpcRespDTO().buildFailResp(null);
        }
    }

    @Override
    public RpcRespDTO<PaginationRespDTO<User>> findByUsername(String username, int page) {
        PaginationRespBO<User> userList = null;
        try {
            userList = testUserService.findByUsername(username, page);
            PaginationRespDTO<User> userPaginationRespDTO = new PaginationRespDTO<User>();
            userPaginationRespDTO.setList(userList.getList());
            userPaginationRespDTO.setTotal(userList.getTotal());
            return new RpcRespDTO<PaginationRespDTO<User>>().buildSuccessResp(userPaginationRespDTO);
        } catch (ServiceException e) {
            return new RpcRespDTO().buildFailResp(null);
        }
    }
}
